package com.jxwifi.cloud.quickclean.popwindow;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.PopAreaAdapter;
import com.jxwifi.cloud.quickclean.adapter.PopAsswmAdapter;
import com.jxwifi.cloud.quickclean.assemble.AssembleDetailsActivity;
import com.jxwifi.cloud.quickclean.assemble.AssembleMainFilter;
import com.jxwifi.cloud.quickclean.bean.AssemRecordsBean;
import com.jxwifi.cloud.quickclean.bean.AssembleItemsBean;
import com.popwindow.BasicPopmodule;

import java.util.List;

public class PopAssemTypeModle extends BasicPopmodule {

    private String TAG = this.getClass().getSimpleName();
    private Context mContext;
    //private PopTypeListener mPoplistener;
    private PopAsswmAdapter mAdapter;
    private List<AssembleItemsBean> mListData;
    private List<AssemRecordsBean> listRecords;
    private String mPopTitle;
    private AssembleMainFilter mListener;

    public PopAssemTypeModle(Context context,String title, List<AssembleItemsBean> data,List<AssemRecordsBean> listRecords){
        this.mContext=context;
        //this.mPoplistener=l;
        this.mPopTitle=title;
        this.mListData=data;
        this.listRecords = listRecords;
        initView();
    }

    public void initAssemble(AssembleMainFilter listener){
        mListener = listener;
    }

    @Override
    public void setPopwindow(PopupWindow pop) {
        mPopwindows = pop;
    }

    @Override
    public View getView() {
        return mContentView;
    }

    private void initView() {
        mContentView = LayoutInflater.from(mContext).inflate(R.layout.pop_assem_type, new LinearLayout(mContext), false);
        setListener();
    }

    private void setListener() {
        View mVwPopBlan = (View) mContentView.findViewById(R.id.vw_pop_blank);
        TextView mTvPopCancel = (TextView) mContentView.findViewById(R.id.tv_pop_assem_cancel);
        TextView mTvPopTitle = (TextView) mContentView.findViewById(R.id.tv_assem_type_title);
        RecyclerView mRvPopModule = (RecyclerView) mContentView.findViewById(R.id.rv_pop_assem_list);
        mTvPopTitle.setText("选中你要拼团的"+mPopTitle);
        mRvPopModule.setLayoutManager(new GridLayoutManager(mContext, 2));
        mRvPopModule.setAdapter(mAdapter = new PopAsswmAdapter(R.layout.pop_assem_list_item));
        mAdapter.setNewData(mListData);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //poplistener.AreaClickTv(position);

                for (int i = 0; i < listRecords.size(); i++) {
                    if (listRecords.get(i).getProductName().equals(mListData.get(position).getOptionName())){
                        //拼单列表已经存在该商品拼团
                        //Log.i(TAG,"拼单列表已经存在该商品拼团  "+listRecords.get(i).getProductName());
                        mListener.filterExistAssemble(mListData.get(position).getOptionName(),i,listRecords.get(i).getCdesk());
                        mPopwindows.dismiss();
                        return;
                    }
                }
                Intent intent=new Intent();
                intent.setClass(mContext, AssembleDetailsActivity.class);
                intent.putExtra("assemble_details", mListData.get(position));
                //Log.i(TAG,"你选中的商品是 "+mListData.get(position).getOptionName());
                intent.putExtra("assemble_details_name", mPopTitle);
                mContext.startActivity(intent);
                mPopwindows.dismiss();
            }
        });
        mVwPopBlan.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });
        mTvPopCancel.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });
    }

    /*public interface PopTypeListener {
        void AssemType(String OrderTime);

    }*/
}
